/*
	Filename	:	bjAction.cpp
	Author(s)	:	Ray E. Bornert II
	Date		:	2002-MAY-16
	Copyright	:	(c) 2002 RealTimeGaming

	History		:
*/

//////////////
// INCLUDES //
//////////////
#include "bjaction.h"
#include <string.h>
#include "bjrules.h"
#include "cardset.h"

BJAction::BJAction()
{
	memset(this,0,sizeof(BJAction));
}

BJAction::~BJAction()
{
}

void BJAction::SetAction
(
	char		who,
	GameAction	action,
	int			minHands,
	int			maxHands,
	int			minCards,
	int			maxCards,
	int			minTotal,
	int			maxTotal,
	int			minSoft,
	int			maxSoft,
	char*		pPattern2,
	char*		pPattern //21 cards
)
{
	strncpy(&m_pattern[0], pPattern, sizeof(m_pattern) );
	strncpy(&m_pattern2[0], pPattern2, sizeof(m_pattern2) );

	m_who			= who;
	m_action		= action;
	m_minHands		= minHands;
	m_maxHands		= maxHands;
	m_minCards		= minCards;
	m_maxCards		= maxCards;
	m_minTotal		= minTotal;
	m_maxTotal		= maxTotal;
	m_minSoft		= minSoft;
	m_maxSoft		= maxSoft;
}

int BJAction::IsPattern()
{
	return (m_pattern[0] != 0);
}

int BJAction::IsPattern2()
{
	return (m_pattern2[0] != 0);
}

int BJAction::IsAllowed
(
	char		who,
	GameAction	action,
	int			nHands,
	int			nCards,
	int			total,
	int			soft,
	unsigned char* pCards
)
{
	if (who		== m_who)
	if (action	== m_action)
	if (nHands	>= m_minHands)
	if (nHands	<= m_maxHands)
	if (nCards	>= m_minCards)
	if (nCards	<= m_maxCards)
	if (total	>= m_minTotal)
	if (total	<= m_maxTotal)
	if (soft	>= m_minSoft)
	if (soft	<= m_maxSoft)
	{
		//player first card pattern
		if (IsPattern2())
		if (!IsCardsInSet( pCards, 1, (unsigned char*)&m_pattern2[0] ))
			return 0;

		//no pattern present
		if (IsPattern())
		if (!IsCardsInSet( pCards, nCards, (unsigned char *)&m_pattern[0] ))
			return 0;

		return 1;
	}

	return 0;

}


int BJAction::IsAllowed
(
	char		who,
	GameAction	action,
	BJHand*		pHand,
	int			nHands
)
{
	if (pHand == NULL)
		return 0;

	//get the number of cards in the hand
	int nCards	= pHand->getCount();

	//get the total of the hand
	int total	= pHand->GetScore();

	//cards
	unsigned char* pCards = pHand->getCards();

	int soft	= pHand->IsSoft();

	int allowed = IsAllowed
	(
		who,
		action,
		nHands,
		nCards,
		total,
		soft,
		pCards
	);

	return allowed;

}

int BJAction::IsOffered
(
	char		who,
	GameAction	action
)
{
	if (m_who		== who)
	if (m_action	== action)
		return 1;

	return 0;
}